function wave(id,w,h) {
	var ctx;
	var cW=w;
	var cH=h;
	var vertexes = [];
	var diffPt = [];
	const oldDiff = 800;
	var autoDiff = oldDiff;
	var verNum = 100;
	var xx = 100;
	var dd = 20;
	var per=0;
	var showH=0;
	function init (callback) {
		ctx = wx.createCanvasContext(id);
		for(var i = 0;i < verNum;i++)
			vertexes[i] = new Vertex(cW / (verNum -1) * i , cH,cH);
		initDiffPt();
		showH= 0;
		var FPS = 30;
		var interval = 1000 / FPS >> 0;
		var timer = setInterval( update, interval );
	}
	function initDiffPt(){
		for(var i=0;i<verNum;i++)
		   diffPt[i]= 0;
	}
	function update(){
		ctx.clearRect(0, 0, cW, cH);
		if(autoDiff<=1E-15)autoDiff = oldDiff;
		autoDiff -= autoDiff*0.9;
		diffPt[xx] = autoDiff;
		//左侧
		//差分，使得每个点都是上一个点的下一次的解，由于差分函数出来的解是一个曲线，且每次迭代后，曲线相加的结果形成了不断地波浪
			for(var i=xx-1;i>0;i--)
			{
				var d = xx-i;
				if(d > dd)d=dd;
				diffPt[i] -= (diffPt[i]-diffPt[i+1])*(1-0.01*d);
			}
		//右侧
			// for(var i=xx+1;i<verNum;i++)
			// {
			// 	var d = i-xx;
			// 	if(d > dd)d=dd;
			// 	diffPt[i] -= (diffPt[i]-diffPt[i-1])*(1-0.01*d);
			// }
		//更新点Y坐标
		for(var i = 0;i < vertexes.length;i++){
			vertexes[i].updateY(diffPt[i]);
		}
		draw();
	}
	function draw(){
		var len = vertexes.length;
		ctx.beginPath();
		ctx.moveTo(0,cH);
		ctx.setFillStyle(getColor(per));
		ctx.lineTo(vertexes[0].x,vertexes[0].y);
		for(var i = 1;i < len;i++){
			ctx.lineTo(vertexes[i].x,vertexes[i].y);
		}
		ctx.lineTo(cW,cH);
		ctx.lineTo(0,cH);
		ctx.fill();
		ctx.draw();
	}
	function Vertex(x,y,baseY){
		this.baseY = baseY;
		this.x = x;
		this.y = y;
		this.vy = 0;
		this.targetY = 0;
		this.friction = 0.15;
		this.deceleration = 0.8;
	}
	Vertex.prototype.updateBase = function(val){
		this.baseY = val;
	}
	Vertex.prototype.updateY = function(diffVal){
		this.targetY = diffVal + this.baseY;
		this.vy += this.targetY - this.y;
		this.y += this.vy * this.friction;
		this.vy *= this.deceleration;
	}
	function getColor(per){
		let num = per*1;
		if(num<=0.6){
			return "#A4A4A6";
		}else if(num<=0.7){
			return "#54BCAC";
		}else if(num<=0.8){
			return "#9FCC61";
		}else if(num<=0.9){
			return "#F6B326";
		}else if(num<=1){
			return "#F5504A";
		}
	}
	function getShowHeight(num){
		if(num<=0.6){
			return 0.9*cH;
		}else if(num<=0.7){
			return 0.7*cH;
		}else if(num<=0.8){
			return 0.5*cH;
		}else if(num<=0.9){
			return 0.3*cH;
		}else if(num<=1){
			return 0.1*cH;
		}
	}

	function step(p){
		per = p;
		let showH = getShowHeight(p);
		for(var i=0;i<vertexes.length;++i){
			vertexes[i].updateBase(showH);
		}
	}

	function start () {
		init();
		// needAnimate = true;
		// setTimeout(function () {
		// 	if (needAnimate){ 
		// 		animate();
		// 	}
		// }, 1000);
	}
	function stop () {
		needAnimate = false;
	}
	return {start: start, stop: stop,step:step};
};
	
	// wave.start();
module.exports={
	wave:wave	
}